package com.linewow.xhyy.nihaonew.activity;

import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.entity.baidu.BaiduInfo;
import com.linewow.xhyy.nihaonew.entity.baidu.TranslateInfo;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/10/26.
 */
public class TranslateActivity extends BaseActivity {
    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.translate_content_tv)
    TextView translateContentTv;
    private String TAG="TranslateActivity";

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_translate);
        ButterKnife.bind(this);
        toolbarBackTitle.setText(getString(R.string.title_translate));
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent=getIntent();
        String info=intent.getStringExtra("info");
        requestInfo(info);

    }

    private void requestInfo(String info) {
        showLoading();

        Log.e(TAG,"翻译的字段--->"+info);
        info=info.replace("\n","***");
        info=info.trim();
        NiHaoOkgo.baiduInfo(info)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaiduInfo<TranslateInfo>>() {
                    @Override
                    public void call(BaiduInfo<TranslateInfo> translateInfoBaiduInfo) {
                        dismissLoading();
                        if(translateInfoBaiduInfo.trans_result!=null){
                            String translate=translateInfoBaiduInfo.trans_result.get(0).dst.replace("***","\n");
                            translateContentTv.setText(translate);
                        }
                    }
                });


    }


    @OnClick(R.id.toolbar_back_left)
    public void onClick() {
        finish();
    }
}
